package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.DailyResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.HealthIndexProtocol

class HealthIndexPresenter(context: Context, mView: HealthIndexProtocol.IView): BasePresenter(context), HealthIndexProtocol.IPresenter {
    private var mView: HealthIndexProtocol.IView = mView
    private var addDailyIsBusying = false

    override fun getDaily(sday: String) {
        if (addDailyIsBusying) return
        addDailyIsBusying = true
        OperationManager.getInstancce().getDaily(sday, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                addDailyIsBusying = false
                if (state == 200) {
                    val data = GsonBuilder().create().fromJson<ArrayList<DailyResponse>>(
                        GsonBuilder().create().toJson(obj), object : TypeToken<ArrayList<DailyResponse>>(){}.type)
                    mView?.getDailySuccessed(data)
                }
            }

            override fun onFail(code: Int, message: String) {
                addDailyIsBusying = false
                mView?.getDailyFailed(code, message)
            }

        })
    }

}